#include "master.h"
#include <dshow.h>
#include "menu.h"
#include "stream.h"
#include "comfunct/comfunct.h"

void ChangeMenuState(HWND hwnd, UINT flag)
{
    HMENU hParentMenu = GetMenu(hwnd), hFileMenu = NULL;
	EnableMenuItem(hParentMenu, CONTROL_MENU, MF_BYPOSITION | flag);
	EnableMenuItem(hParentMenu, VOLUME_MENU, MF_BYPOSITION | flag);
	hFileMenu = GetSubMenu(hParentMenu, FILE_MENU);
	EnableMenuItem(hFileMenu, IDI_FILE_CLOSE, MF_BYCOMMAND | flag);
	DrawMenuBar(hwnd);
	return;
}

void MenuCheck(HWND hwnd, int iMenu, UINT uMenuItem, bool bCheck)
{
     HMENU hParentMenu = GetMenu(hwnd), hSubMenu = NULL;
	 hSubMenu = GetSubMenu(hParentMenu, iMenu);
     
     if(bCheck == true)
     {
          CheckMenuItem(hSubMenu, uMenuItem, MF_BYCOMMAND | MF_CHECKED);
     }
     else
     {
          CheckMenuItem(hSubMenu, uMenuItem, MF_BYCOMMAND | MF_UNCHECKED);    
     }
     DrawMenuBar(hwnd);
     return;
}

HMENU MakePopupMenu(HWND hwnd, const bool bLoopAudio, IMediaControl* pControl, PLAYLISTDATA* pld)
{
     HMENU hPopup = NULL;
	 OAFilterState fsState = 0;
	 POINT pt = {0};
     MENUITEMINFO mii[12] = {{0}};
	 int i = 0;

	 for(; i < 12; ++i)
	 {
		 mii[i].cbSize = sizeof(MENUITEMINFO);
	 }

     mii[0].fMask = MIIM_ID |  MIIM_TYPE | MIIM_STATE;
     mii[0].fType = MFT_STRING;
     mii[0].fState = MFS_DEFAULT;
     mii[0].wID = IDI_TRAY_SHOW_WINDOW;
     mii[0].dwTypeData = (LPTSTR)GetLocalText(9);
     //mii[0].cch = 17;
     
     mii[1].fMask = MIIM_ID | MIIM_TYPE;
     mii[1].fType = MFT_STRING;
     mii[1].wID = IDI_FILE_OPEN;
     mii[1].dwTypeData = (LPTSTR)GetLocalText(10);
     //mii[1].cch = 24;

	 mii[2].fMask = MIIM_ID | MIIM_TYPE | MIIM_SUBMENU;
     mii[2].fType = MFT_STRING;
     mii[2].hSubMenu = CreatePlaylistMenu(pld);
     mii[2].dwTypeData = (LPTSTR)GetLocalText(11);
     //mii[2].cch = 9;
     
     mii[3].cbSize = sizeof(MENUITEMINFO);
     mii[3].fMask = MIIM_TYPE;
     mii[3].fType = MFT_SEPARATOR;
     
     mii[4].fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE;
     mii[4].fType = MFT_STRING;
     mii[4].wID = IDI_CONT_PLAY;
     mii[4].dwTypeData = (LPTSTR)GetLocalText(1);
     //mii[4].cch = 5;
     
     mii[5].fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE;
     mii[5].fType = MFT_STRING;
     mii[5].wID = IDI_CONT_PAUSE;
     
     if(pControl != NULL)
     {
         pControl->lpVtbl->GetState(pControl, 10, &fsState);
         if(fsState != State_Paused)
         {
             mii[5].dwTypeData = (LPTSTR)GetLocalText(2);
             //mii[5].cch = 6;
         }
         else
         {
             mii[5].dwTypeData = (LPTSTR)GetLocalText(12);
             //mii[5].cch = 7;
         }
         mii[5].fState = MFS_ENABLED;
     }
     else
     {
         mii[5].dwTypeData = (LPTSTR)GetLocalText(2);
         //mii[5].cch = 6;
     }
     
     mii[6].fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE;
     mii[6].fType = MFT_STRING;
     mii[6].wID = IDI_CONT_STOP;
     mii[6].dwTypeData = (LPTSTR)GetLocalText(3);
     //mii[6].cch = 5;
     
     mii[7].fMask = MIIM_ID | MIIM_TYPE;
     mii[7].fType = MFT_STRING;
     mii[7].wID = IDI_CONT_LOOP;
     mii[7].dwTypeData = (LPTSTR)GetLocalText(13);
     //mii[7].cch = 5;
     
     mii[8].fMask = MIIM_TYPE;
     mii[8].fType = MFT_SEPARATOR;
     
     mii[9].fMask = MFT_STRING | MIIM_SUBMENU | MIIM_TYPE | MIIM_STATE;
     mii[9].hSubMenu = CreateVolumeSubMenu();
     mii[9].dwTypeData = (LPTSTR)GetLocalText(4);
     //mii[9].cch = 7;
     
     mii[10].fMask = MIIM_TYPE;
     mii[10].fType = MFT_SEPARATOR;
     
     mii[11].fMask = MIIM_ID | MIIM_TYPE;
     mii[11].fType = MFT_STRING;
     mii[11].wID = IDI_FILE_EXIT;
     mii[11].dwTypeData = (LPTSTR)GetLocalText(14);
     //mii[11].cch = 17;

	 if(pControl == NULL)
	 {
		 mii[4].fState = MFS_GRAYED;
		 mii[5].fState = MFS_GRAYED;
		 mii[6].fState = MFS_GRAYED;
		 mii[9].fState = MFS_GRAYED;
	 }
     
     hPopup = CreatePopupMenu();
     InsertMenuItem(hPopup, 0, true, &mii[0]);
     InsertMenuItem(hPopup, 1, true, &mii[1]);
     InsertMenuItem(hPopup, 2, true, &mii[2]);
     InsertMenuItem(hPopup, 3, true, &mii[3]);
     InsertMenuItem(hPopup, 4, true, &mii[4]);
     InsertMenuItem(hPopup, 5, true, &mii[5]);
     InsertMenuItem(hPopup, 6, true, &mii[6]);
     InsertMenuItem(hPopup, 7, true, &mii[7]);
     InsertMenuItem(hPopup, 8, true, &mii[8]);
     InsertMenuItem(hPopup, 9, true, &mii[9]);
     InsertMenuItem(hPopup, 10, true, &mii[10]);
	 InsertMenuItem(hPopup, 11, true, &mii[11]);
     
     if(bLoopAudio == true)
     {
          CheckMenuItem(hPopup, IDI_CONT_LOOP, MF_BYCOMMAND | MF_CHECKED);
     }
     
	 if(pControl != NULL) /* This wraps the GetMute() call because calling that function when DShow
						     isn't initialised causes an access violation trying to get the current volume */
	 {
		 if(GetMute() == true)
		 {
			 CheckMenuItem(hPopup, IDI_VOL_MUTE, MF_BYCOMMAND | MF_CHECKED);
		 }
	 }
     
     SetForegroundWindow(hwnd);
     GetCursorPos(&pt);
     TrackPopupMenuEx(hPopup, TPM_VERTICAL | TPM_LEFTALIGN | TPM_RIGHTBUTTON, pt.x, pt.y, hwnd, NULL);
     return hPopup;
}

HMENU CreateVolumeSubMenu()
{
	HMENU hVolMenu = NULL;
	MENUITEMINFO miiVolume[3] = {{0}};
	 short i = 0;
	 for(; i < 3; ++i)
	 {
		 miiVolume[i].cbSize = sizeof(MENUITEMINFO);
	 }
     
     miiVolume[0].fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE;
     miiVolume[0].fType = MFT_STRING;
     miiVolume[0].wID = IDI_VOL_MUTE;
     miiVolume[0].dwTypeData = (LPTSTR)GetLocalText(15);
     miiVolume[0].cch = 5;
     
     miiVolume[1].fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE;
     miiVolume[1].fType = MFT_STRING;
     miiVolume[1].wID = IDI_VOL_HALF;
     miiVolume[1].dwTypeData = (LPTSTR)TEXT("50%");
     miiVolume[1].cch = 4;
     
     miiVolume[2].fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE;
     miiVolume[2].fType = MFT_STRING;
     miiVolume[2].wID = IDI_VOL_FULL;
     miiVolume[2].dwTypeData = (LPTSTR)TEXT("100%");
     miiVolume[2].cch = 5;
     
     hVolMenu = CreatePopupMenu();
     InsertMenuItem(hVolMenu, 0, true, &miiVolume[0]);
     InsertMenuItem(hVolMenu, 1, true, &miiVolume[1]);
     InsertMenuItem(hVolMenu, 2, true, &miiVolume[2]);

     return hVolMenu;
}

HMENU CreatePlaylistMenu(PLAYLISTDATA* pld)
{
	 HMENU hPlaylistMenu = NULL;
	 MENUITEMINFO miiPlaylist[4] = {{0}};
	 short i = 0;
	 for(; i < 4; ++i)
	 {
		 miiPlaylist[i].cbSize = sizeof(MENUITEMINFO);
	 }

	 miiPlaylist[0].fMask = MIIM_ID | MIIM_TYPE;
	 miiPlaylist[0].fType = MFT_STRING;
	 miiPlaylist[0].wID = IDI_FILE_PLAYLIST_OPEN;
	 miiPlaylist[0].dwTypeData = (LPTSTR)GetLocalText(16);
	 miiPlaylist[0].cch = 16;

	 miiPlaylist[1].fMask = MIIM_ID | MIIM_TYPE;
     miiPlaylist[1].fType = MFT_STRING;
     miiPlaylist[1].wID = IDI_PLAYLIST_SHUFFLE;
     miiPlaylist[1].dwTypeData = (LPTSTR)GetLocalText(17);
     miiPlaylist[1].cch = 8;

	 miiPlaylist[2].fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE;
     miiPlaylist[2].fType = MFT_STRING;
     miiPlaylist[2].wID = IDI_PLAYLIST_NEXT;
     miiPlaylist[2].dwTypeData = (LPTSTR)GetLocalText(18);
     miiPlaylist[2].cch = 5;

	 if(pld->lNumOfFilesinPlaylist == 0)
	 {
		 miiPlaylist[2].fState = MFS_GRAYED;
	 }

	 miiPlaylist[3].fMask = MIIM_ID | MIIM_TYPE;
	 miiPlaylist[3].fType = MFT_STRING;
	 miiPlaylist[3].wID = IDI_PLAYLIST_SAVEM3U;
	 miiPlaylist[3].dwTypeData = (LPTSTR)GetLocalText(19);
	 miiPlaylist[3].cch = 29;

	 hPlaylistMenu = CreatePopupMenu();
     InsertMenuItem(hPlaylistMenu, 0, true, &miiPlaylist[0]);
     InsertMenuItem(hPlaylistMenu, 1, true, &miiPlaylist[1]);
	 InsertMenuItem(hPlaylistMenu, 2, true, &miiPlaylist[2]);
	 InsertMenuItem(hPlaylistMenu, 3, true, &miiPlaylist[3]);

	 if(pld->bShuffle == true)
	 {
		 CheckMenuItem(hPlaylistMenu, IDI_PLAYLIST_SHUFFLE, MF_BYCOMMAND | MF_CHECKED);
	 }
	 
	 return hPlaylistMenu;
}

HMENU MakePViewMenu(HWND hwnd, POINT pt)
{
	MENUITEMINFO mii[6] = {0};
	HMENU hPopup = NULL;
	short i = 0;
	for(; i < 6; ++i)
	{
		mii[i].cbSize = sizeof(MENUITEMINFO);
		mii[i].fMask = MIIM_ID | MIIM_TYPE;
	}

	mii[0].fMask |= MIIM_STATE;
	mii[0].fType = MFT_STRING;
	mii[0].fState = MFS_DEFAULT;
	mii[0].wID = IDI_PV_PLAY_TRACK;
	mii[0].dwTypeData = (LPTSTR)GetLocalText(20);
	//mii[0].cch = 16;

	mii[1].fType = MFT_STRING;
	mii[1].wID = IDI_PV_OPEN_FOLDER;
	mii[1].dwTypeData = (LPTSTR)GetLocalText(21);
	//mii[1].cch = 23;

	mii[2].fType = MFT_STRING;
	mii[2].wID = IDI_PV_COPY_TITLE;
	mii[2].dwTypeData = (LPTSTR)GetLocalText(22);
	//mii[2].cch = 17;

	mii[3].fType = MFT_STRING;
	mii[3].wID = IDI_PV_COPY_PATH;
	mii[3].dwTypeData = (LPTSTR)GetLocalText(23);
	//mii[3].cch = 16;

	mii[4].fType = MFT_STRING;
	mii[4].wID = IDI_PV_REMOVE_TRACK;
	mii[4].dwTypeData = (LPTSTR)GetLocalText(24);
	//mii[4].cch = 27;

	mii[5].fType = MFT_STRING;
	mii[5].wID = IDI_PV_DELETE_TRACK;
	mii[5].dwTypeData = (LPTSTR)GetLocalText(25);
	//mii[5].cch = 23;

	hPopup = CreatePopupMenu();
	InsertMenuItem(hPopup, 0, true, &mii[0]);
	InsertMenuItem(hPopup, 1, true, &mii[1]);
	InsertMenuItem(hPopup, 2, true, &mii[2]);
	InsertMenuItem(hPopup, 3, true, &mii[3]);
	InsertMenuItem(hPopup, 4, true, &mii[4]);
	InsertMenuItem(hPopup, 5, true, &mii[5]);
	TrackPopupMenuEx(hPopup, TPM_VERTICAL | TPM_LEFTALIGN | TPM_RIGHTBUTTON, pt.x, pt.y, hwnd, NULL);
	return hPopup;
}
